<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Font;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class FontController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {


        return Grid::make(new Font(), function (Grid $grid) {

            $grid->column('name');
            $grid->column('path');
            $grid->column('status')->switch();
            $grid->column('remarks','備註')->editable();
            $grid->column('created_at');
            $grid->showQuickEditButton();
            $grid->disableEditButton();
            $grid->enableDialogCreate();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Font(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('path');
            $show->field('format');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Font(), function (Form $form) {

            $form->text('name')->required();
            $form->file('path')->autoUpload()->maxSize(102400)->move('font')->uniqueName()->required()->accept('ttf');
            $form->textarea('remarks','備註');
            $form->switch('status')->default(1);
            $form->hidden('format');
            $form->saving(function (Form $form) {
                $format = [
                    'ttf'=>'truetype',
                    'woff'=>'woff',
                    'woff2'=>'woff2',
                    'svg'=>'svg',
                    'otf'=>'opentype',
                    'eot'=>'embedded-opentype',
                ];
                $extension = array_get(pathinfo($form->path),'extension');
                $form->format = array_get($format,$extension,$extension);
            });
        });
    }
}
